using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using Microsoft.Surface.Presentation.Input;
using Microsoft.Win32;

namespace BoardGamePlayer
{
    /// <summary>
    /// Interaction logic for SurfaceWindow1.xaml
    /// </summary>
    public partial class SurfaceWindow1 : SurfaceWindow
    {
        /// <summary>
        /// Default constructor.
        /// </summary>
        public SurfaceWindow1()
        {
            InitializeComponent();

            // Add handlers for window availability events
            AddWindowAvailabilityHandlers();
        }

        /// <summary>
        /// Occurs when the window is about to close. 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            // Remove handlers for window availability events
            RemoveWindowAvailabilityHandlers();
        }

        /// <summary>
        /// Adds handlers for window availability events.
        /// </summary>
        private void AddWindowAvailabilityHandlers()
        {
            // Subscribe to surface window availability events
            ApplicationServices.WindowInteractive += OnWindowInteractive;
            ApplicationServices.WindowNoninteractive += OnWindowNoninteractive;
            ApplicationServices.WindowUnavailable += OnWindowUnavailable;
        }

        /// <summary>
        /// Removes handlers for window availability events.
        /// </summary>
        private void RemoveWindowAvailabilityHandlers()
        {
            // Unsubscribe from surface window availability events
            ApplicationServices.WindowInteractive -= OnWindowInteractive;
            ApplicationServices.WindowNoninteractive -= OnWindowNoninteractive;
            ApplicationServices.WindowUnavailable -= OnWindowUnavailable;
        }

        /// <summary>
        /// This is called when the user can interact with the application's window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnWindowInteractive(object sender, EventArgs e)
        {
            //TODO: enable audio, animations here
        }

        /// <summary>
        /// This is called when the user can see but not interact with the application's window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnWindowNoninteractive(object sender, EventArgs e)
        {
            //TODO: Disable audio here if it is enabled

            //TODO: optionally enable animations here
        }

        /// <summary>
        /// This is called when the application's window is not visible or interactive.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnWindowUnavailable(object sender, EventArgs e)
        {
            //TODO: disable audio, animations here
        }

        private void addTokenButton_Click(object sender, RoutedEventArgs e)
        {
            Token t = new Token();
            t.TokenBrush = ((Ellipse)((SurfaceButton)sender).Content).Fill;
            playField.Items.Add(t);
        }

        private void addCustomDeckButton_Click(object sender, RoutedEventArgs e)
        {
            string cardsPath;
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            System.Windows.Forms.MessageBox.Show("Please select a directory containing your card images.");
            fbd.ShowDialog();
            cardsPath = fbd.SelectedPath;
            if (cardsPath == "") return;

            string backPath = "";
            System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
            ofd.ShowDialog();
            backPath = ofd.FileName;
            if (backPath == "") return;
            Deck d = new Deck(cardsPath, backPath);
            d.shuffleDeck();
            playField.Items.Add(d);
        }

        private void trashRectangle_Drop(object sender, SurfaceDragDropEventArgs e)
        {
            //drop it into oblivion
        }

        private void expander_TouchUp(object sender, TouchEventArgs e)
        {
            (sender as Expander).IsExpanded = !(sender as Expander).IsExpanded;
        }

        private void changeBackgroundColor(Brush color) 
        {
            backgroundRectangle.Fill = color;
        }

        private void ChangeColor_Click(object sender, RoutedEventArgs e)
        {
            changeBackgroundColor(((Rectangle)((SurfaceButton)sender).Content).Fill);
        }

        private void changeBackgroundImageButton_Click(object sender, RoutedEventArgs e)
        {
            string imgPath = "";
            System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
            ofd.ShowDialog();
            imgPath = ofd.FileName;
            if (imgPath == "") return;
            ImageBrush imgBrush = null;
            try
            {
                imgBrush = new ImageBrush(new BitmapImage(new Uri(imgPath)));
            }
            catch (Exception ex) 
            {
                return;
            }
            changeBackgroundColor(imgBrush);
        }

        private void addDieButton_Click(object sender, RoutedEventArgs e)
        {
            playField.Items.Add(new Dice());
        }

        private void addCounterButton_Click(object sender, RoutedEventArgs e)
        {
            playField.Items.Add(new Counter());
        }
    }
}